package vip.web3.admin.sys.service.impl.base;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RBucket;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Service;
import vip.web3.admin.sys.service.base.RedissonClientService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Description TODO RedissonClient
 * aut-horIcarus
 * @Date 2022/6/6 16:35
 * @Version 1.0
 */
@ConditionalOnClass(RedissonClient.class)
@Slf4j
@Service
public class RedissonClientServiceImpl implements RedissonClientService {

    @Resource
    private RedissonClient redissonClient;

    @Override
    public boolean trySet(String key, int value, int time, TimeUnit timeUnit) {
        return redissonClient.getBucket(key).trySet(value, time, timeUnit);
    }
    
    @Override
    public boolean trySet(String key, Object value) {
        return redissonClient.getBucket(key).trySet(value);
    }

    @Override
    public void setValue(String key, Object value, Long time, TimeUnit timeUnit) {
        redissonClient.getBucket(key).set(value, time, timeUnit);
    }

    @Override
    public void setValue(String key, Object value) {
        redissonClient.getBucket(key).set(value);
    }

    @Override
    public boolean hasKey(String key) {
        return null != redissonClient.getBucket(key).get();
    }

    @Override
    public boolean deleteKey(String key) {
        RBucket<Object> bucket = redissonClient.getBucket(key);
        if (bucket.isExists()) {
            return bucket.delete();
        }
        return true;
    }

    @Override
    public Long incr(String key) {
        return redissonClient.getAtomicLong(key).incrementAndGet();
    }

    @Override
    public Long getRAtomicLong(String key) {
        return redissonClient.getAtomicLong(key).get();
    }

    @Override
    public Long incrAddAndGet(String key, long value) {
        return redissonClient.getAtomicLong(key).addAndGet(value);
    }

    @Override
    public Integer getInt(String key) {
        RBucket<String> bucket = redissonClient.getBucket(key);
        String str = bucket.get();
        return StringUtils.isBlank(str) ? 0 : Integer.parseInt(str);
    }

    @Override
    public String getString(String key) {
        RBucket<String> bucket = redissonClient.getBucket(key);
        return bucket.get();
    }

    @Override
    public boolean getBucketLock(String key, Long id) {
        String lockName = key + ":" + id;
        RBucket<String> bucket = redissonClient.getBucket(lockName);
        return bucket.trySet(id.toString(), 5, TimeUnit.SECONDS);
    }

    @Override
    public void releaseBucketLock(String key, Long id) {
        String lockName = key + ":" + id;
        RBucket<String> bucket = redissonClient.getBucket(lockName);
        if (bucket.isExists()) {
            bucket.delete();
        }
    }

    @Override
    public boolean setExpire(String key, Long time, TimeUnit timeUnit) {
        return redissonClient.getBucket(key).expire(time, timeUnit);
    }

    @Override
    public boolean setExpireDate(String key, Date date) {
        return redissonClient.getBucket(key).expireAt(date);
    }

    @Override
    public RBloomFilter<String> getBloomFilter(String str) {
        RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter(str);
        return bloomFilter;
    }

    @Override
    public RLock getLock(String s) {
        return redissonClient.getLock(s);
    }
}
